package com.heima.reggie.controller;

import com.heima.reggie.common.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

/*
@RestController
public class FileController2 {
    @Value("${reggie_path}")
    private String loadPath;

    @PostMapping("/common/upload")
    public R upload(MultipartFile file) {
        String replace = UUID.randomUUID().toString().replace("-", "");
        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));

        String newName = replace + substring;
        File file1 = new File(loadPath);
        if (!file1.exists()) {
            file1.mkdirs();
        }

        File file2 = new File(file1, newName);

        try {
            file.transferTo(file2);
            return R.success(newName);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("上传失败！");
        }

    }

    @GetMapping("/common/download")
    public void download(String name, HttpServletResponse response) throws Exception {
        FileInputStream inputStream = new FileInputStream(loadPath + name);
        ServletOutputStream outputStream = response.getOutputStream();

        response.setContentType("image/jpg");

        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        inputStream.close();

    }
}
*/
